package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import com.itheima.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetmealController {

  @Autowired
  SetMealService setmealService;
  @Autowired
    SetmealDishService setmealDishService;

  @Autowired
    CategoryService categoryService;
  @Autowired
  DishService dishService;

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);
        return  R.success("新增套餐成功!");

    }

    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
      Page<SetmealDto> pageInfo = setmealService.pageWithName(page, pageSize, name);

      return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> delete(Long[] ids){

        setmealService.deleteWithDish(ids);

        return R.success("删除成功!");
    }

    //更改套餐状态
    @PostMapping("status/{status}")
    public R<String> status(@PathVariable Integer status,Long[] ids){
      setmealService.updateStatus(status,ids);
        return R.success("更改套餐状态成功!");
    }

    @GetMapping("{id}")
   public R<Setmeal>  getById(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);

//        Setmeal setmeal = setmealService.getById(id);
//        return R.success(setmeal);
   }

   @PutMapping
   @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);
        return R.success("更新套餐成功!");
   }

   @GetMapping("list")
   @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId")
    public R<List<Setmeal>> list(Setmeal setmeal) {

       LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
               .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
               .eq(Setmeal::getStatus,1)
               .orderByDesc(Setmeal ::getUpdateTime);
       List<Setmeal> setmealList = setmealService.list(queryWrapper);

       return R.success(setmealList);

   }

   @GetMapping("dish/{id}")
    public R<List<SetmealDish>>   setmealDish(@PathVariable Long id){

       LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
               .eq(SetmealDish::getId, id);

       List<SetmealDish> dishList = setmealDishService.list(queryWrapper);


       return R.success(dishList);
   }
}
